import os

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "about_orm.settings")
import django

django.setup()

from app01 import models

from django.db.models import Max, Min, Count, Avg, Sum

# 统计书中最大的价格
# ret= models.Book.objects.filter(pk__range=[3,7]).aggregate(max=Max('price'),min=Min('price'))
# print(ret)


# 统计每一本书的作者个数
# ret =models.Book.objects.annotate(count=Count('authors'))  # annotate 注释

# 统计出每个出版社的最便宜的书的价格
# 方法一
# ret =models.Publisher.objects.annotate(min=Min('book__price'))

# 方法二
# ret = models.Book.objects.values('pub__id').annotate(min=Min('price'))


# 统计不止一个作者的图书
# ret = models.Book.objects.annotate(count=Count('authors')).filter(count__gt=1).values()
# print(ret)

# 查询各个作者出的书的总价格
# ret = models.Author.objects.annotate(sum=Sum('books__price'))
# for i in ret:
#     print(i.name,i.sum)

ret = models.Book.objects.values('authors__name').annotate(Sum('price'))
print(ret)